package services

import (
    "bankend/database"
    "bankend/models"
    "time"
)

// UserSubscribeService 用户订阅服务
type UserSubscribeService struct{}

// GetUserSubscribe 获取用户订阅设置
func (s *UserSubscribeService) GetUserSubscribe(userID int) (*models.UserSubscribe, error) {
    var subscribe models.UserSubscribe
    err := database.DB.Where("user_id = ? AND deleted_at IS NULL", userID).First(&subscribe).Error
    return &subscribe, err
}

// CreateUserSubscribe 创建用户订阅设置
func (s *UserSubscribeService) CreateUserSubscribe(subscribe *models.UserSubscribe) error {
    return database.DB.Create(subscribe).Error
}

// UpdateUserSubscribe 更新用户订阅设置
func (s *UserSubscribeService) UpdateUserSubscribe(subscribe *models.UserSubscribe) error {
    return database.DB.Model(&models.UserSubscribe{}).
        Where("user_id = ? AND deleted_at IS NULL", subscribe.UserID).
        Updates(map[string]interface{}{
            "subscribe_web":    subscribe.SubscribeWeb,
            "subscribe_wechat": subscribe.SubscribeWechat,
        }).Error
}

// DeleteUserSubscribe 删除用户订阅设置（软删除）
func (s *UserSubscribeService) DeleteUserSubscribe(userID int) error {
    return database.DB.Model(&models.UserSubscribe{}).
        Where("user_id = ? AND deleted_at IS NULL", userID).
        Update("deleted_at", time.Now()).Error
}